# 系统配置相关视图
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from LogisticsCard.models import SystemConfig, Log


@login_required
def system_config_view(request):
    """
    系统配置视图，展示和修改系统配置。
    :param request: HttpRequest对象
    :return: 渲染系统配置页面或保存配置
    """
    configs = SystemConfig.objects.all()
    msg = ''
    if request.method == 'POST':
        for c in configs:
            new_value = request.POST.get(c.key, c.value)
            if c.value != new_value:
                c.value = new_value
                c.save()
        msg = '保存成功！'
    return render(request, 'system_config/index.html', {'config': configs, 'msg': msg})
